import csv,os,time,re,urllib

dateval=str(time.localtime()[2])

data=[{},{},{},{},{},{}]
timere=re.compile('\d{4}')
cancre=re.compile('CANCELLED')
validre=re.compile('LANDED|AIRBORNE')
writers=[]

from BeautifulSoup import BeautifulSoup

class Flight:
   """Simple class to hold individual flight details.
These can then be accessed by:
<flight>.airportname
<flight>.flightno
<flight>.expected (time)
<flight>.location (source or destination)
<flight>.status  (current status of flight)"""
   def __init__(self,airportname,flightno,expected,location,status):
      """Initialise Flight class"""
      self.airportname=airportname
      self.flightno=flightno
      self.expected=expected
      self.location=location
      self.status=status

class HtmlClass:
   """Class to hold html data and to return
a 'soup' of parsed html.
<HtmlClass>.url - address of html
<HtmlClass>.name - airport code
<HtmlClass>.fd - file descriptor for open url
<HtmlClass>.soup - parsed 'soup' of html"""
   html=[]
   soup=[]
   fd=[]
   def __init__(self,code,address):
      self.url=address
      self.name=code
      self.fd=urllib.urlopen(self.url)
      self.html=self.fd.read()
      self.fd.close()
      self.soup=BeautifulSoup(self.html)

#class instantiations for arrivals and departures for three airports
edinarr=HtmlClass("EDI","http://www.edinburghairport.com/portal/page/Edinburgh%5EGeneral%5EFlight+information%5ELive+flight+arrivals/")
glasarr=HtmlClass("GLA","http://www.glasgowairport.com/portal/page/Glasgow%5EGeneral%5EFlight+information%5ELive+flight+arrivals/")
aberarr=HtmlClass("ABZ","http://www.aberdeenairport.com/portal/page/Aberdeen%5EGeneral%5EFlight+information%5ELive+flight+arrivals/")
edindep=HtmlClass("EDI","http://www.edinburghairport.com/portal/page/Edinburgh%5EGeneral%5EFlight+information%5ELive+flight+departures/")
glasdep=HtmlClass("GLA","http://www.glasgowairport.com/portal/page/Glasgow%5EGeneral%5EFlight+information%5ELive+flight+departures/")
aberdep=HtmlClass("ABZ","http://www.aberdeenairport.com/portal/page/Aberdeen%5EGeneral%5EFlight+information%5ELive+flight+departures/")

#a list of all airports for loop construct
allairports=[edinarr,edindep,glasarr,glasdep,aberarr,aberdep]
alltypes=['Arr','Dep']*3
allcodes=['EDI','GLA','ABZ']
readfiles=[]
writefiles=[]



if os.path.exists('/home/scm-studs/f6100782/python/EDI-'+dateval+'09-Arr-f6100782.csv'):
   for code,typeof in zip(allcodes*2,alltypes):
      readfiles.append(open(code+'-'+dateval+'09-'+typeof+'-f6100782.csv','r'))
   for code,typeof,dic in zip(allcodes*2,alltypes,range(0,5)):
      for row in csv.reader(readfiles[dic],delimiter=','):
         data[dic][row[0]]=row
   for fd in readfiles:
      fd.close()


#for each airport
for airport in allairports:
   #remove date information
   dates=airport.soup.tbody.findAll('th')
   for date in dates:
      date.extract()
      #and also remove table heading
      airport.soup.tbody.contents[1].extract()
      #for each flight section at each airport, arrivals AND departures
      for flights in airport.soup.tbody:
         #if it's not a valid flight line, ignore
         valid=False
         if len(flights) < 5:
            pass
         else:
            #store flight details
            if cancre.search(str(flights.contents[7].contents[0]).strip()) \
			!=None:
               timeval='XXXX'
               valid=True
            elif validre.search(str(flights.contents[7].contents[0]) \
			.strip())!=None:
                 timeval=timere.search(str(flights.contents[7]. \
				contents[0].strip())).group()
                 valid=True
            else:
               pass
            if valid==True:
               data[allairports.index(airport)] \
                     [str(flights.contents[3].contents[0])]= \
                      [str(flights.contents[3].contents[0]), \
			str(flights.contents[5].contents[0]).strip(), \
			str(flights.contents[1].contents[0]),timeval]

for code,typeof in zip(allcodes*2,alltypes):
   writefiles.append(open('/home/scm-studs/f6100782/python/'+code+'-'+dateval+'09-'+typeof+'-f6100782.csv','wb'))
   
for code,typeof,num in zip(allcodes*2,alltypes,range(0,6)):
   writers.append(csv.writer(writefiles[num],delimiter=','))

for code,typeof,num in zip(allcodes*2,alltypes,range(0,6)):
   for info in data[num]:
      writers[num].writerow(data[num][info])

for fd in writefiles:
   fd.close()

   
